package sharetime.user.service;

import org.hibernate.validator.InvalidValue;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import sharetime.core.AnnotationValidator;
import sharetime.user.domain.User;

@SuppressWarnings("unchecked")
public class UserValidator implements Validator {

	public boolean supports(Class clazz) {
		return User.class.isAssignableFrom(clazz);
	}

	public void validate(Object target, Errors errors) {
		User user = (User) target;

		InvalidValue[] invalids = AnnotationValidator.getInvalidValues(user);

		for (InvalidValue invalidValue : invalids) {
			errors.rejectValue(invalidValue.getPropertyPath(), null,
					invalidValue.getMessage());
		}
	}

}
